<?php

  /**
   * @package JooS
   * @subpackage PHPackager_Logger
   */
  class JooS_PHPackager_Logger {
    private static $_instance = null;

    public static function Observer_beforeAddPackages() {
      self::newInstance()->log("Packages installation started");
    }

    public static function Observer_afterAddPackages() {
      self::newInstance()->log("Packages installation complete");
    }

    public static function Observer_beforeRemovePackage() {
      self::newInstance()->log("Package removing started");
    }

    public static function Observer_afterRemovePackage($result) {
      self::newInstance()->log("Package removing " . ($result ? "complete" : "failed"));
    }

    private static function newInstance() {
      if (self::$_instance === null)
        self::$_instance = new self();
      return self::$_instance;
    }

    protected function __construct() {
      require_once "JooS/PHPackager/Manager.php";

      JooS_Event::newInstance(JooS_PHPackager_Manager::EVENT_PACKAGE_NEW_INSTANCE)
        ->attach(array($this, "Observer_onPackageNewInstance"), true);

      JooS_Event::newInstance(JooS_PHPackager_Manager::EVENT_PACKAGE_INSTALLED)
        ->attach(array($this, "Observer_onPackageInstalled"), true);

      JooS_Event::newInstance(JooS_PHPackager_Manager::EVENT_PACKAGE_UPGRADED)
        ->attach(array($this, "Observer_onPackageUpgraded"), true);

      JooS_Event::newInstance(JooS_PHPackager_Manager::EVENT_PACKAGE_COMMITED)
        ->attach(array($this, "Observer_onPackageCommited"), true);
    }

    protected function log($message) {
      echo $message . "\n";
    }

    public function Observer_onPackageNewInstance($package) {
      $this->log( "New package instance created: " . $package->getName() . " v" . $package->getVersion() );
    }

    public function Observer_onPackageInstalled($package) {
      $this->log( "Package " . $package->getName() . " v" . $package->getVersion() . " installed" );
    }

    public function Observer_onPackageUpgraded($NEW_package, $OLD_package) {
      $this->log( "Package " . $NEW_package->getName() . " upgraded from v" . $OLD_package->getVersion() . " to " . $NEW_package->getVersion() );
    }

    public function Observer_onPackageCommited($package) {
      $this->log( "Package " . $package->getName() . " v" . $package->getVersion() . " commited into system" );
    }
  }
